//
//  MJTStatusTableViewCell.swift
//  swiftWeiBo
//
//  Created by MJT on 16/7/13.
//  Copyright © 2016年 MJT. All rights reserved.
//

import UIKit
import SnapKit
//  首页cell子控件的间距
let HomeCellMargin: CGFloat = 10
class MJTStatusTableViewCell: UITableViewCell {

    //记录toolbar的顶部约束
    var toolbarTopConstraint: Constraint?

    //使用ViewModel模型
    var statusViewModel: MJTStatusViewModel? {
        didSet {
            //给原创微博设置需要的视图模型
            originalView.statusViewModel = statusViewModel
            //卸载约束
            toolbarTopConstraint?.uninstall()
            //判断是否有转发微博对象
            if statusViewModel?.status?.retweeted_status != nil {
                retweetView.statusViewModel = statusViewModel
                //有转发数据,更新约束
                retweetView.hidden = false
                toolBar.snp_updateConstraints(closure: { (make) -> Void in
                    //更新约束
                    self.toolbarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
                })
            }else {
                //没有转发微博数据
                retweetView.hidden = true
                toolBar.snp_updateConstraints(closure: { (make) -> Void in
                    self.toolbarTopConstraint = make.top.equalTo(originalView.snp_bottom).constraint
                })
            }
            //给toolBar视图设置需要的视图模型
            toolBar.statusViewModel = statusViewModel
        }
    }


    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //MARK: -- 懒加载控件
    //原创微博视图
    private lazy var originalView: MJTStatusOriginalView = MJTStatusOriginalView()
    //toolBar
    private lazy var toolBar: MJTStatusToolBar = MJTStatusToolBar()
    // 转发微博
    private lazy var retweetView: MJTStatusRetweetView = MJTStatusRetweetView()
    private func setupUI() {
        contentView.backgroundColor = UIColor(white: 0.95, alpha: 1)
        contentView.addSubview(originalView)
        contentView.addSubview(toolBar)
        contentView.addSubview(retweetView)

        originalView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView).offset(8)
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
        }
        retweetView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom)
            make.leading.equalTo(originalView)
            make.trailing.equalTo(originalView)
        }
        toolBar.snp_makeConstraints { (make) -> Void in
            self.toolbarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
            make.height.equalTo(35)
        }
        contentView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(toolBar)
            make.top.equalTo(self)
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
        }
    }
}
